$import( "SmitearWebGL.Programs.AbstractProgram" );

SmitearWebGL.Programs.Hdr.BloomProgram = SmitearWebGL.Programs.AbstractProgram.extend(
{
	/******** constructor ********/
	
	init : function()
	{
		this._super();
		this._registClass( "SmitearWebGL.Programs.Hdr.BloomProgram" );
	}
	,
	
	/******** public method ********/
	
	activeUniform : function( renderableObject )
	{
		this.context.gl.uniformMatrix4fv( this._shader.uniforms.tcLumOffsets.location, false, renderableObject._offsets );
		this.context.gl.uniform1f( this._shader.uniforms.uBrightPassThreshold.location, SmitearWebGL.Objects.Canvas.Hdr.Bloom.brightThreshold );
		
		var texture = renderableObject.getTextureAt( 0 );
		this.context.gl.activeTexture( this.context.gl.TEXTURE0 );
		this.context.gl.bindTexture( this.context.gl.TEXTURE_2D, texture.getGlTexture() );
		this.context.gl.uniform1i( this._shader.uniforms.uOriginal.location, 0 );
		
//		var texture = renderableObject.getMaterial()._layers[1].getTexture();
//		this.context.gl.activeTexture( this.context.gl.TEXTURE1 );
//		this.context.gl.bindTexture( this.context.gl.TEXTURE_2D, texture.getGlTexture() );
//		this.context.gl.uniform1i( this._shader.uniforms.uAvelumMap.location, 1 );
		
		return SmitearWebGL.Core.HResult.SUCCESS;
	}
	,
	
	/******** private method ********/
	
	_generateVertexShaderStr : function()
	{
		this._super();
		
		var vertStr =
		"\
		attribute vec3 " + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION +";\n\
		varying vec2 vTexCoord;\n\
		void main(void)\n\
		{\n\
			vec4 pos = vec4(" + SmitearWebGL.Resources.Buffer.AttribLocation.POSITION +", 1.0);\n\
			gl_Position = pos;\n\
			vTexCoord = vec2((pos.x + 1.0) * 0.5, (pos.y + 1.0) * 0.5);\n\
		}\n\
		";
		
		return vertStr;
	}
	,
	_generateFragmentShaderStr : function()
	{
		this._super();
		
		var fragStr = 
		"\
		#ifdef GL_ES\n\
		precision highp float;\n\
		#endif\n\
		varying vec2 vTexCoord;\n\
		uniform mat4 tcLumOffsets;\n\
		uniform sampler2D uOriginal;\n\
		uniform float uBrightPassThreshold;\n\
		const vec4 lumfact = vec4(0.27,0.67,0.06,0.0);\n\
		void main(void) {\n\
			vec4 average = vec4(0.0, 0.0, 0.0, 0.0);\n\
			vec4 color = vec4(0.0, 0.0, 0.0, 0.0);\n\
			vec2 offsets[4];\n\
			offsets[0] = vec2(tcLumOffsets[0][0], tcLumOffsets[0][1]);\n\
			offsets[1] = vec2(tcLumOffsets[1][0], tcLumOffsets[1][1]);\n\
			offsets[2] = vec2(tcLumOffsets[2][0], tcLumOffsets[2][1]);\n\
			offsets[3] = vec2(tcLumOffsets[3][0], tcLumOffsets[3][1]);\n\
		    for(int i = 0; i < 4; i++)\n\
		    {\n\
		    	color = texture2D( uOriginal, vTexCoord + offsets[i] );\n\
				color.rgb *= 1.0 / color.a;\n\
		        average += vec4(color.rgb, 1.0);\n\
		    }\n\
		    average *= 0.25;\n\
		    float luminance = dot( average, lumfact );\n\
		    if( luminance < uBrightPassThreshold )\n\
		        average = vec4(0.0, 0.0, 0.0, 1.0);\n\
		    gl_FragColor = average;\n\
		}\n\
		";
		
		return fragStr;
	}
}
);